package com.card.manager.services.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.card.framework.mybatis.mapper.BatchCallBack;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.mybatis.optimist.ModifiedByAnotherUserException;
import com.card.framework.page.model.PageModel;
import com.card.manager.dao.AaaMapper;
import com.card.manager.dao.AbaMapper;
import com.card.manager.dao.AbbMapper;
import com.card.manager.dao.CardOpDayMapper;
import com.card.manager.dao.CardOpMonthMapper;
import com.card.manager.dao.CardOpYearMapper;
import com.card.manager.dao.MabMapper;
import com.card.manager.dao.MafMapper;
import com.card.manager.domain.Aaa;
import com.card.manager.domain.AaaExample;
import com.card.manager.domain.Aba;
import com.card.manager.domain.AbaExample;
import com.card.manager.domain.AbaExample.Criteria;
import com.card.manager.domain.Abb;
import com.card.manager.domain.CardOpDay;
import com.card.manager.domain.CardOpMonth;
import com.card.manager.domain.CardOpYear;
import com.card.manager.domain.Mab;
import com.card.manager.domain.MabExample;
import com.card.manager.domain.Maf;
import com.card.manager.services.AbaService;

@Service("AbaServiceImpl")
public class AbaServiceImpl extends MyFrameworkSupperMapper implements AbaService{
	
	public List<Aba> selectAbaForPage(Long userid) {
		AbaMapper mapper = this.getMapper(AbaMapper.class);
		AbaExample abaExample = new AbaExample();
		Criteria oredCriteria  = abaExample.createCriteria();
		//
		oredCriteria.andUseridEqualTo(userid);
		oredCriteria.andDelflgEqualTo((byte)1);
		List<Aba> list = mapper.selectByExample(abaExample);
	    return list;
	}
	
	
	public Aba geyAbaById(String abaid) {
		AbaMapper mapper = this.getMapper(AbaMapper.class);
		AbaExample abaExample = new AbaExample();
		Criteria oredCriteria  = abaExample.createCriteria();
		//
		oredCriteria.andAbaidEqualTo(abaid);
		Aba aba = mapper.selectByPrimaryKey(abaid);
		if(aba==null){
			aba = new Aba();
			aba.setMessage("卡片未激活.");
		}else if(aba.getLosted()!=1){
			aba.setMessage("卡片已冻结.");
		}else{
			aba.setMessage("检索成功.");
		}
	    return aba;
	}
	
	@Override
	public void deleteAbas(final List<Aba> aba) throws ModifiedByAnotherUserException,Exception {
		batchSubmit(AbaMapper.class,new BatchCallBack() {
			@Override
			public <E> void batchCallBack(E mapper) {
				if(aba != null && aba.size() > 0) {
					for(Aba a :aba){
						a.setDelflg((byte)0);
						((com.card.manager.dao.AbaMapper) mapper).updateByPrimaryKeySelective(a);
					}
				}
			}
		});
	}

	@Override
	@Transactional
	public void addOrUpdate(Aba aba,Abb abb) {
		AbaMapper mapper = this.getMapper(AbaMapper.class);
		AbbMapper abbMapper = this.getMapper(AbbMapper.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssS");
		String itemId = sdf.format(new Date());
		abb.setId(itemId);
		//Aba queryResult = mapper.selectByPrimaryKey(aba.getUserid());
		if(aba.getVersion()==null){
			abb.setEmoneybalance(0.0);
			abb.setMoneybalance(0.0);
			abb.setFlag((byte)1);
			abb.setCreditflag((byte)3);
			aba.setLosted(1);
			aba.setVersion((long)1);
			aba.setDelflg((byte)1);
			mapper.insert(aba);
		}else
		{
			AbaExample abaExample = new AbaExample();
			abaExample.createCriteria().andAbaidEqualTo(aba.getAbaid());
			mapper.updateByExampleSelective(aba, abaExample);
			abb.setFlag((byte)1);
		}
		abbMapper.insert(abb);
	}


	@Override
	public Map<String, Object> queryCardInfo(Aaa aaa, Aba aba) {
		AaaMapper aaaMapper = this.getMapper(AaaMapper.class);
		AaaExample aaaExample = new AaaExample();
		com.card.manager.domain.AaaExample.Criteria oredCriteria  = aaaExample.createCriteria();
		Criteria oredCriteriaAba = aaaExample.aba.createCriteria();
		if(StringUtils.isNotEmpty(aaa.getOwnercard())) 
			oredCriteria.andOwnercardEqualTo(aaa.getOwnercard());
		if(StringUtils.isNotEmpty(aba.getAbaid())) 
			oredCriteriaAba.andAbaidEqualTo(aba.getAbaid());
		oredCriteriaAba.andDelflgEqualTo((byte)1);
		List<Aaa> aaaList = aaaMapper.selectUserInfoForCardChange(aaaExample);
		if(aaaList==null || aaaList.size()<1){
			return  new HashMap<String, Object>();
		}else
		{
			Map<String, Object> modelMap = new HashMap<String, Object>();
		    modelMap.put("userInfo", aaaList.get(0));  
		    return modelMap;
		}

	}
	@Override
	@Transactional
	public void changeCard(Aba oldAba, Aba newAba,Mab mab) {
		AbaMapper mapper = this.getMapper(AbaMapper.class);
		AbaExample abaExample = new AbaExample();
		Criteria tempCriteria = abaExample.createCriteria();
		tempCriteria.andAbaidEqualTo(oldAba.getAbaid());
		mapper.updateByExampleSelective(oldAba, abaExample);
		mapper.insert(newAba);
		MabMapper mabMapper = this.getMapper(MabMapper.class);
		mabMapper.insert(mab);
		
	}

	@Override
	@Transactional
	public void CardOper(Aba aba,Maf maf) {
		AbaMapper mapper = this.getMapper(AbaMapper.class);
		mapper.updateByPrimaryKeySelective(aba);
		MafMapper mafmapper = this.getMapper(MafMapper.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssS");
		String itemId = sdf.format(new Date());
		maf.setItemid(itemId);
		mafmapper.insert(maf);
	}


	@Override
	public Map<String, Object> queryChangeCardForPage(Mab mab,PageModel pageModel) {
		MabMapper mapper = this.getMapper(MabMapper.class);
		MabExample mabExample = new MabExample();
		com.card.manager.domain.MabExample.Criteria oredCriteria  = mabExample.createCriteria();
		if(pageModel!=null)
		{
			mabExample.setPage(pageModel);
			if(StringUtils.isNotEmpty(pageModel.getSort())) 
				mabExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());			
		}
		if(StringUtils.isNotEmpty(mab.getItemid())) 
			oredCriteria.andItemidEqualTo(mab.getItemid());
		if(StringUtils.isNotEmpty(mab.getOldcardid())) 
			oredCriteria.andOldcardidEqualTo(mab.getOldcardid());
		if(StringUtils.isNotEmpty(mab.getNewcardid())) 
			oredCriteria.andNewcardidEqualTo(mab.getNewcardid());
		if(StringUtils.isNotEmpty(mab.getCreatuserid())) 
			oredCriteria.andCreatuseridEqualTo(mab.getCreatuserid());
		if(StringUtils.isNotEmpty(mab.getStartDate()))
		{	
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = new Date();
			try {
				startDate = sdf.parse(mab.getStartDate());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			oredCriteria.andCreatdatetimeGreaterThanOrEqualTo(startDate);
		}
		if(StringUtils.isNotEmpty(mab.getEndDate()))
		{	
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date endDate = new Date();
			try {
				endDate = sdf.parse(mab.getEndDate()+"23:59:59");
			} catch (ParseException e) {
				e.printStackTrace();
			}
			oredCriteria.andCreatdatetimeLessThanOrEqualTo(endDate);
		}
		List<Mab> list = mapper.selectByExample(mabExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
		if(pageModel!=null)
		{
			modelMap.put("total", pageModel.getTotalCount());  		
		}
	    modelMap.put("rows", list); 
	    mabExample.setPage(null);
	    MabMapper  mabMapper = this.getMapper(MabMapper.class);
	    List<Mab> footer = mabMapper.sumByExample(mabExample);
	    modelMap.put("footer", footer); 
	    return modelMap;
	}


	@Override
	public Map<String, Object> queryCardOpYearForPage(CardOpYear tempParam,
			PageModel pageModel) {
		CardOpYearMapper cardOpYearMapper = this.getMapper(CardOpYearMapper.class);
		com.card.manager.domain.CardOpYearExample cardOpYearExample = new com.card.manager.domain.CardOpYearExample();
		com.card.manager.domain.CardOpYearExample.Criteria oredCriteria  = cardOpYearExample.createCriteria();
		if(StringUtils.isNotEmpty(tempParam.getOperator())) 
			oredCriteria.andOperatorEqualTo(tempParam.getOperator());
		if(tempParam.getStartValue()!=null&&tempParam.getStartValue()>0)
		{
			oredCriteria.andYearGreaterThanOrEqualTo(tempParam.getStartValue());
		}
		if(tempParam.getEndValue()!=null&&tempParam.getEndValue()>0)
		{
			oredCriteria.andYearLessThanOrEqualTo(tempParam.getEndValue());
		}
		if(pageModel!=null)
		{
			cardOpYearExample.setPage(pageModel);
			if(StringUtils.isNotEmpty(pageModel.getSort())) 
				cardOpYearExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());			
		}
		List<CardOpYear> list = cardOpYearMapper.selectByExample(cardOpYearExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
		if(pageModel!=null)
		{
			modelMap.put("total", pageModel.getTotalCount());  		
		}
	    modelMap.put("rows", list); 
	    cardOpYearExample.setPage(null);
	    List<CardOpYear> footer = cardOpYearMapper.sumByExample(cardOpYearExample);
	    modelMap.put("footer", footer);
	    return modelMap;
	}


	@Override
	public Map<String, Object> queryCardOpMonthForPage(CardOpMonth tempParam,
			PageModel pageModel) {
		CardOpMonthMapper cardOpMonthMapper = this.getMapper(CardOpMonthMapper.class);
		com.card.manager.domain.CardOpMonthExample cardOpMonthExample = new com.card.manager.domain.CardOpMonthExample();
		com.card.manager.domain.CardOpMonthExample.Criteria oredCriteria  = cardOpMonthExample.createCriteria();
		if(StringUtils.isNotEmpty(tempParam.getOperator())) 
			oredCriteria.andOperatorEqualTo(tempParam.getOperator());
		if(tempParam.getStartValue()!=null&&tempParam.getStartValue()>0)
		{
			oredCriteria.andMonthGreaterThanOrEqualTo(tempParam.getStartValue());
		}
		if(tempParam.getEndValue()!=null&&tempParam.getEndValue()>0)
		{
			oredCriteria.andMonthLessThanOrEqualTo(tempParam.getEndValue());
		}
		if(pageModel!=null)
		{
			cardOpMonthExample.setPage(pageModel);
			if(StringUtils.isNotEmpty(pageModel.getSort())) 
				cardOpMonthExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());			
		}
		List<CardOpMonth> list = cardOpMonthMapper.selectByExample(cardOpMonthExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
		if(pageModel!=null)
		{
			modelMap.put("total", pageModel.getTotalCount());  		
		}
	    modelMap.put("rows", list); 
	    cardOpMonthExample.setPage(null);
	    List<CardOpMonth> footer = cardOpMonthMapper.sumByExample(cardOpMonthExample);
	    modelMap.put("footer", footer);
	    return modelMap;
	}


	@Override
	public Map<String, Object> queryCardOpDayForPage(CardOpDay tempParam,
			PageModel pageModel) {
		CardOpDayMapper cardOpDayMapper = this.getMapper(CardOpDayMapper.class);
		com.card.manager.domain.CardOpDayExample cardOpDayExample = new com.card.manager.domain.CardOpDayExample();
		com.card.manager.domain.CardOpDayExample.Criteria oredCriteria  = cardOpDayExample.createCriteria();
		if(StringUtils.isNotEmpty(tempParam.getOperator())) 
			oredCriteria.andOperatorEqualTo(tempParam.getOperator());
		if(tempParam.getStartValue()!=null&&tempParam.getStartValue()>0)
		{
			oredCriteria.andDayGreaterThanOrEqualTo(tempParam.getStartValue());
		}
		if(tempParam.getEndValue()!=null&&tempParam.getEndValue()>0)
		{
			oredCriteria.andDayLessThanOrEqualTo(tempParam.getEndValue());
		}
		if(pageModel!=null)
		{
			cardOpDayExample.setPage(pageModel);
			if(StringUtils.isNotEmpty(pageModel.getSort())) 
				cardOpDayExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());			
		}
		List<CardOpDay> list = cardOpDayMapper.selectByExample(cardOpDayExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
		if(pageModel!=null)
		{
			modelMap.put("total", pageModel.getTotalCount());  		
		}
	    modelMap.put("rows", list); 
	    cardOpDayExample.setPage(null);
	    List<CardOpDay> footer = cardOpDayMapper.sumByExample(cardOpDayExample);
	    modelMap.put("footer", footer);
	    return modelMap;
	}


	@Override
	public void cardProcUser(Map temp) {
		CardOpDayMapper cardOpDayMapper = this.getMapper(CardOpDayMapper.class);
		cardOpDayMapper.cardProcUser(temp);
	}

}